//
//  DashedLineView.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/20.
//  
  

#import "DashedLineView.h"

@implementation DashedLineView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        _lineColor = [UIColor blackColor];
        _lineWidth = 1.5;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {
    // 创建 CAShapeLayer
    CAShapeLayer *dashedLineLayer = [CAShapeLayer layer];
    
    // 设置路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    if (_direction == DLDirectionVertical) {
        [path moveToPoint:CGPointMake(CGRectGetWidth(rect)*0.5, 0)]; // 起点
        [path addLineToPoint:CGPointMake(CGRectGetWidth(rect)*0.5, CGRectGetHeight(rect))]; // 终点
    } else {
        [path moveToPoint:CGPointMake(0, CGRectGetHeight(rect)*0.5)]; // 起点
        [path addLineToPoint:CGPointMake(CGRectGetWidth(rect), CGRectGetHeight(rect)*0.5)]; // 终点
    }
    
    // 设置虚线样式
    dashedLineLayer.path = path.CGPath;
    dashedLineLayer.strokeColor = _lineColor.CGColor; // 虚线颜色
    dashedLineLayer.lineWidth = _lineWidth; // 虚线宽度
    dashedLineLayer.lineDashPattern = @[@4, @2]; // 虚线样式：4个点实线，2个点空白
    
    // 添加到视图的图层
    [self.layer addSublayer:dashedLineLayer];
}

@end
